﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace nb.development.csmediamanager
{
    public partial class ImageEditor : Form
    {
        public ImageEditor(Bitmap image, string filename)
        {
            InitializeComponent();
            this.image = image;
            this.tmpMap = image;
            this.filename = filename;
            panel3.Width = image.Width;
            panel3.Height = image.Height;
            label1.Text = panel3.Width + "x" + panel3.Height;
            effects = new Effects();
            effects.EffectChanged += new EffectChangeHandler(effects_EffectChanged);
            effects.FormClosed += new FormClosedEventHandler(effects_FormClosed);
            resizer = new Resize(image.Height, image.Width);
            resizer.Resized += new ResizeChangeHandler(resizer_Resized);
            resizer.FormClosed += new FormClosedEventHandler(resizer_FormClosed);
            quality = new Quality(imgmode, jpgquality);
            quality.QualityChanged += new QualityChangeHandler(quality_QualityChanged);
            quality.FormClosed += new FormClosedEventHandler(quality_FormClosed);
        }

        void quality_FormClosed(object sender, FormClosedEventArgs e)
        {
            barQuality.Pushed = false;
            quality = new Quality(imgmode, jpgquality);
            quality.QualityChanged += new QualityChangeHandler(quality_QualityChanged);
            quality.FormClosed += new FormClosedEventHandler(quality_FormClosed);
        }

        private imagemode imgmode = imagemode.PNG;
        private int jpgquality = 100;
        private string filename;

        public string Filename
        {
            get { return filename; }
        }

        void quality_QualityChanged()
        {
            imgmode = quality.QualityMode;
            jpgquality = quality.JPGQuality;
        }

        void resizer_FormClosed(object sender, FormClosedEventArgs e)
        {
            barResize.Pushed = false;
            resizer = new Resize(tmpMap.Height, tmpMap.Width);
            resizer.Resized += new ResizeChangeHandler(resizer_Resized);
            resizer.FormClosed += new FormClosedEventHandler(resizer_FormClosed);
        }

        void resizer_Resized()
        {
            panel3.Height = resizer.ImageHeight;
            panel3.Width = resizer.ImageWidth;
            panel3.Refresh();
        }

        void effects_FormClosed(object sender, FormClosedEventArgs e)
        {
            barEffect.Pushed = false;
            effects = new Effects();
            effects.EffectChanged += new EffectChangeHandler(effects_EffectChanged);
            effects.FormClosed += new FormClosedEventHandler(effects_FormClosed);
        }

        void effects_EffectChanged()
        {

            panel3.Refresh();
        }

        private Bitmap image;
        public Bitmap Image
        {
            get { return image; }
        }

        private Bitmap tmpMap;

        private void panel3_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.DrawImage(tmpMap, 0, 0, panel3.Width, panel3.Height);
            if (barEffect.Pushed)
            {
                PointF p = new PointF();
                if (effects.Watermarkposition == ContentAlignment.TopLeft)
                {
                    p.X = 10;
                    p.Y = 10;
                }
                else if (effects.Watermarkposition == ContentAlignment.TopRight)
                {
                    p.Y = 10;
                    p.X = panel3.Width - (e.Graphics.MeasureString(effects.Watermarktext, effects.Watermarkfont).Width + 10);
                }
                else if (effects.Watermarkposition == ContentAlignment.BottomRight)
                {
                    p.Y = panel3.Height - (e.Graphics.MeasureString(effects.Watermarktext, effects.Watermarkfont).Height + 10);
                    p.X = panel3.Width - (e.Graphics.MeasureString(effects.Watermarktext, effects.Watermarkfont).Width + 10);
                }
                else if (effects.Watermarkposition == ContentAlignment.BottomLeft)
                {
                    p.Y = panel3.Height - (e.Graphics.MeasureString(effects.Watermarktext, effects.Watermarkfont).Height + 10);
                    p.X = 10;
                }
                Color c = Color.FromArgb(200, effects.WatermarkColor);
                e.Graphics.DrawString(effects.Watermarktext, effects.Watermarkfont, new SolidBrush(c), p);
            }
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
            if (panel3.Height < panel1.Height)
                panel3.Top = ((panel1.Height - panel3.Height) / 2);
            else panel3.Top = 0;
            if (panel3.Width < panel1.Width)
                panel3.Left = ((panel1.Width - panel3.Width) / 2);
            else panel3.Left = 0;
        }

        private Effects effects;
        private Resize resizer;
        private Quality quality;

        private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            if (e.Button.Name == "barResize" && e.Button.Pushed)
            {
                resizer.Show(this);
                resizer.SetDesktopLocation(this.DesktopLocation.X + this.Width - 280, this.DesktopLocation.Y + 372);
            }
            else if (e.Button.Name == "barResize" && !e.Button.Pushed)
            {
                resizer.Hide();
            }
            else if (e.Button.Name == "barQuality" && e.Button.Pushed)
            {
                quality.Show(this);
                quality.SetDesktopLocation(this.DesktopLocation.X + this.Width - 280, this.DesktopLocation.Y + 472);
            }
            else if (e.Button.Name == "barQuality" && !e.Button.Pushed)
            {
                quality.Hide();
            }
            else if (e.Button.Name == "barEffect" && e.Button.Pushed)
            {
                effects.Show(this);
                effects.SetDesktopLocation(this.DesktopLocation.X + this.Width - 280, this.DesktopLocation.Y + 65);
            }
            else if (e.Button.Name == "barEffect" && !e.Button.Pushed)
            {
                effects.Hide();
            }
            else if (e.Button.Name == "barReset")
            {
                tmpMap = image;
                panel3.Width = image.Width;
                panel3.Height = image.Height;
            }
            else if (e.Button.Name == "barCancel")
            {
                DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else if (e.Button.Name == "barDone")
            {
                Bitmap b = new Bitmap(panel3.Width, panel3.Height);
                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.Transparent);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawImage(tmpMap, 0, 0, panel3.Width, panel3.Height);
                if (barEffect.Pushed)
                {
                    PointF p = new PointF();
                    if (effects.Watermarkposition == ContentAlignment.TopLeft)
                    {
                        p.X = 10;
                        p.Y = 10;
                    }
                    else if (effects.Watermarkposition == ContentAlignment.TopRight)
                    {
                        p.Y = 10;
                        p.X = panel3.Width - (g.MeasureString(effects.Watermarktext, effects.Watermarkfont).Width + 10);
                    }
                    else if (effects.Watermarkposition == ContentAlignment.BottomRight)
                    {
                        p.Y = panel3.Height - (g.MeasureString(effects.Watermarktext, effects.Watermarkfont).Height + 10);
                        p.X = panel3.Width - (g.MeasureString(effects.Watermarktext, effects.Watermarkfont).Width + 10);
                    }
                    else if (effects.Watermarkposition == ContentAlignment.BottomLeft)
                    {
                        p.Y = panel3.Height - (g.MeasureString(effects.Watermarktext, effects.Watermarkfont).Height + 10);
                        p.X = 10;
                    }
                    Color c = Color.FromArgb(200, effects.WatermarkColor);
                    g.DrawString(effects.Watermarktext, effects.Watermarkfont, new SolidBrush(c), p);
                }
                g.Flush();
                if (quality.QualityMode == imagemode.JPG)
                {
                    EncoderParameters eps = new EncoderParameters(1);
                    eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality.JPGQuality);
                    ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
                    filename = filename.Remove(filename.LastIndexOf('.')) + ".jpg";
                    b.Save(filename, ici, eps);
                }
                else
                {
                    filename = filename.Remove(filename.LastIndexOf('.')) + ".png";
                    b.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
                }
                b.Dispose();
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
    }
}
